package com.hk.housekeeping.order.wxPay.controller;

import com.hk.housekeeping.order.utils.ResponseEntity;
import com.hk.housekeeping.order.utils.ResponseUtils;
import com.hk.housekeeping.order.wxPay.config.PayConfiguration;
import com.hk.housekeeping.order.wxPay.entity.Account;
import com.hk.housekeeping.order.wxPay.utils.PayHelper;
import com.hk.housekeeping.order.wxPay.utils.WXPayCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/wxPay/payUrl")
public class PayUrlController extends HttpServlet {
    private PayHelper payHelper = new PayHelper(new PayConfiguration());

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String payId = req.getParameter("payId");
        //返回支付链接
        ResponseEntity<String> entity = null;
        //登录验证
//        Account account = AccountUtils.verifyAccount(req);
        Account account = new Account("abc123","zs","123456789");
        if (payId != null && account != null) {
            String payUrl = payHelper.createPayUrl(Long.parseLong(payId));
            entity = ResponseEntity.success(payUrl);
        } else{
            entity = ResponseEntity.error(WXPayCode.PAY_URL_ERROR);
        }

        ResponseUtils.deliver(resp, entity);
    }
}
